package com.firebird.app.kotlin.stock.ui.index_fragment.deal

import android.app.Dialog
import android.os.Bundle
import android.os.Handler
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import butterknife.BindView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.firebird.app.kotlin.stock.R
import com.firebird.app.kotlin.stock.base.BaseMvpFragment
import com.firebird.app.kotlin.stock.myview.state_layout.OtherView
import com.google.gson.Gson
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout
import com.firebird.app.kotlin.stock.Config
import com.firebird.app.kotlin.stock.data.AllocationManageBean
import com.firebird.app.kotlin.stock.data.EventMessage
import com.firebird.app.kotlin.stock.myview.state_layout.OtherView
import com.firebird.app.kotlin.stock.ui.detailsallocatin.DetailsAllocationActivity
import com.firebird.app.kotlin.stock.ui.godeal.GoDealActivity
import com.firebird.app.kotlin.stock.ui.index_fragment.deal.contract.DealFragmentContract
import com.firebird.app.kotlin.stock.ui.index_fragment.deal.presenter.DealFragmentPresenter
import com.firebird.app.kotlin.stock.ui.nullpage.NullPersonActivity
import com.firebird.app.kotlin.stock.utils.DialogUtils
import com.firebird.app.kotlin.stock.utils.SharedPreferencesUtil
import com.firebird.app.kotlin.stock.utils.ShowDialogUtils
import com.firebird.app.kotlin.stock.utils.ToastUtil
import org.greenrobot.eventbus.EventBus
import java.util.*


class DealFragment : BaseMvpFragment<DealFragmentPresenter?>(), DealFragmentContract.View {
    @BindView(R.id.recycler)
    var recycler: RecyclerView? = null
    @BindView(R.id.otherview)
    var otherview: OtherView? = null
    @BindView(R.id.refresh)
    var refresh: PullToRefreshLayout? = null
    private val list: MutableList<AllocationManageBean.RespDataBean.DataBean> = ArrayList<AllocationManageBean.RespDataBean.DataBean>()
    private var adp: Adp? = null
    private var page = 0
    private var dialog: Dialog? = null
    private var orderId = 0
    private val bundle = Bundle()
    override fun initView(view: View?) {
        otherview!!.setHolder(mHolder)
        mPresenter = DealFragmentPresenter()
        mPresenter.attachView(this)
        mPresenter.getData("1", "", page.toString() + "", Config.PAGESIZE)
        refresh.setRefreshListener(object : BaseRefreshListener() {
            fun refresh() {
                page = 0
                mPresenter.getDataFresh("1", "", page.toString() + "", Config.PAGESIZE)
            }

            fun loadMore() {
                page++
                mPresenter.getDataLoadMore("1", "", page.toString() + "", Config.PAGESIZE)
            }
        })
    }

    override val layoutId: Int
        protected get() = R.layout.fragment_deal

    override fun showLoading() {
        dialog = DialogUtils.showDialog_progressbar(activity)
    }

    override fun hideLoading() {
        DialogUtils.closeDialog(dialog)
    }

    override fun onError(throwable: Throwable?) {
        DialogUtils.closeDialog(dialog)
        refresh.finishRefresh()
        refresh.finishLoadMore()
    }

    fun onSuccess(bean: AllocationManageBean) {
        initData(bean)
    }

    fun onSuccessFresh(bean: AllocationManageBean) {
        refresh.finishRefresh()
        initData(bean)
    }

    fun onSuccessLoadMore(bean: AllocationManageBean) {
        refresh.finishLoadMore()
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            list.addAll(bean.getRespData().getData())
            //延迟更新数据，避免卡顿
            Handler().postDelayed({ adp.notifyItemRangeChanged(0, bean.getRespData().getData().size()) }, 500)
            if (bean.getRespData().getData().size() === 0) {
                ToastUtil.showToast(activity, "没有更多数据")
            }
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(activity)
        } else {
            ToastUtil.showToast(activity, bean.getRespDesc())
        }
    }

    private fun initData(bean: AllocationManageBean) {
        list.clear()
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            list.addAll(bean.getRespData().getData())
            if (list.size == 0) {
                otherview!!.showEmptyView()
                refresh.setCanLoadMore(false)
            } else {
                refresh.setCanLoadMore(true)
                otherview!!.showContentView()
            }
            recycler.setLayoutManager(LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false))
            recycler.setNestedScrollingEnabled(false)
            adp = Adp(R.layout.item_deal, list)
            recycler.setAdapter(adp)
            adp.openLoadAnimation()
            adp.setOnItemChildClickListener(object : OnItemChildClickListener() {
                fun onItemChildClick(adapter: BaseQuickAdapter?, view: View, position: Int) {
                    orderId = list[position].getBpOrderId()
                    bundle.putInt("orderid", orderId)
                    toClass(view.context, GoDealActivity::class.java, bundle)
                }
            })
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(activity)
        } else {
            ToastUtil.showToast(activity, bean.getRespDesc())
        }
    }
}